package cn.b.sky.mes.pp.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.pp.model.condition.OrderTypeCondition;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.pp.model.OrderType;
import cn.b.sky.mes.pp.service.OrderTypeService;

@Controller
@RequestMapping("/orderType/*")
public class OrderTypeController extends SecurityController {
  private static final String ORDER_TYPE_LIST_PAGE = "pp/orderType/orderType_list";

  private static final String ORDER_TYPE_PAGE = "pp/orderType/orderType_add";

  @Autowired
  OrderTypeService orderTypeService;

  @RequestMapping("orderType_list")
  public String getOrderTypesByCondition(@ModelAttribute("condition") OrderTypeCondition condition, String navTabId,
                                         Model model) {
    condition.setSiteCode(getSiteCode());
    condition.setData(orderTypeService.getOrderTypesByCondition(condition));
    condition.setTotalCount(orderTypeService.getOrderTypesCountByCondition(condition));

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("page", condition);
    return ORDER_TYPE_LIST_PAGE;
  }

  @RequestMapping("orderType_add")
  public String addOrderType(String navTabId, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "add");
    return ORDER_TYPE_PAGE;
  }

  @RequestMapping("modifyOrderType")
  public String modifyOrderType(String orderTypeCode, String navTabId, Model model) {
    OrderType orderType = orderTypeService.getOrderType(getSiteCode(), orderTypeCode);

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("orderType", orderType);
    model.addAttribute("pattern", "modify");

    return ORDER_TYPE_PAGE;
  }

  @RequestMapping("viewOrderType")
  public String viewOrderType(String orderTypeCode, String navTabId, Model model) {
    OrderType orderType = orderTypeService.getOrderType(getSiteCode(), orderTypeCode);

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("orderType", orderType);
    model.addAttribute("pattern", "view");

    return ORDER_TYPE_PAGE;
  }

  @RequestMapping("saveOrderType")
  @ResponseBody
  public DWZResult saveOrderType(String id, OrderType orderType, String navTabId, Model model) {
    if (null == orderType || StringUtils.isBlank(orderType.getOrderTypeCode())) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }

    orderType.setSiteCode(getSiteCode());
    setCreateInfo(orderType);
    if (orderType.getLockFlag() == null) {
      orderType.setLockFlag(0);
    }
    if (StringUtils.isBlank(id)) {
      orderTypeService.saveOrderType(orderType);
    } else {
      orderTypeService.modifyOrderType(orderType);
    }
    return dialogAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        navTabId, null, null, null);
  }

  @RequestMapping("deleteOrderType")
  @ResponseBody
  public DWZResult deleteOrderType(String navTabId, Model model, HttpServletRequest request) {
    String orderTypeCodes = request.getParameter("orderTypeCodes");
    if (StringUtils.isBlank(orderTypeCodes)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }

    String[] typeCodes = orderTypeCodes.split(",");
    BatchJobReport[] reports = new BatchJobReport[typeCodes.length];
    int failures = 0;
    for (int i = 0; i < typeCodes.length; i++) {
      reports[i] = orderTypeService.deleteOrderTypeByPk(getSiteCode(), typeCodes[i]);
      if (!reports[i].isSuccess()) {
        failures += 1;
      }
    }

    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

  @RequestMapping("validateOrderTypeCode")
  @ResponseBody
  public boolean validateOrderTypeCode(String orderTypeCode) {
    if (StringUtils.isBlank(orderTypeCode)) {
      return true;
    } else {
      OrderType orderType = orderTypeService.getOrderType(getSiteCode(), orderTypeCode);
      return null == orderType;
    }
  }

  @RequestMapping("checkOrderTypeCode")
  @ResponseBody
  public boolean checkOrderTypeCode(String orderTypeCode) {
    OrderType defectType = orderTypeService.getOrderType(getSiteCode(), orderTypeCode);
    return defectType != null && defectType.getLockFlag() != 1;
  }

}
